//座位路由
const { json } = require("body-parser");
const express = require("express");
const router = express();
const Rooms = require("../model/Rooms")
const Seats = require("../model/Seats");
const Seatslog = require("../model/Seatslog")
const User = require("../model/User")

//分配座位-批量/单独
router.post("/addSeats/:roomId", async(req, res, next) => {
    console.log(req.params)
    console.log(req.body)
    console.log(req.body.length, 111),
    console.log(req.body.filter(item => item.status === 2).length, 2222)
    const totalSeat = req.body.length
    const choosedSeat = req.body.filter(item => item.status === 2).length
    console.log(totalSeat, choosedSeat)
    //return
    await Rooms.updateOne({ "_id": { $in: req.params.roomId } }, { $set: { status: 2, totalSeat: totalSeat, choosedSeat: choosedSeat } })
    await Seats.deleteMany(req.params)
    const data = await Seats.insertMany(req.body);
    res.send({
        status: "ok",
        data: data,
    })
});

//修改座位状态-单独/批量
router.post("/editSeats", async(req, res, next) => {
    console.log(req.body)
    await Seats.updateMany({ "seatKey": { $in: req.body.seatKey } }, { $set: { status: req.body.status } }, { multi: true });
    res.send({
        status: "ok",
    })
});

//用户占座
router.post('/orderSeat/:roomId', async(req, res) => {
    const { seatKey, status } = req.body
    const data = await Seatslog.create(req.body)
    await Seats.findOneAndUpdate({ "seatKey": { $in: seatKey } }, { status: status })
    await Rooms.updateOne({ "_id": { $in: req.params.roomId } }, { $inc: { choosedSeat: 1 }})
    await User.updateOne({ "username": { $in: req.body.username } }, { $set: { status: req.body.status } })
    res.send({
        status: "ok",
        data: data,
    })
})

//用户还座+用户更改预计结束时间
router.post('/returnSeat/:roomId', async(req, res) => {
    const { seatKey, status, _id } = req.body
    await Seatslog.findOneAndUpdate({ "_id": { $in: _id } }, req.body)
    await Seats.findOneAndUpdate({ "seatKey": { $in: seatKey } }, { status: status })
    if(status){
        await Rooms.updateOne({ "_id": { $in: req.params.roomId } }, { $inc: { choosedSeat: -1 }})
    } 
    await User.updateOne({ "username": { $in: req.body.username } }, { $set: { status: req.body.status } })
    res.send({
        status: "ok",
    })
})

//根据教室id查询座位
router.get('/findByRoom/:roomId', async(req, res) => {
    const data = await Seats.find({ 'roomId': req.params.roomId })
    res.send({
        status: "ok",
        data: data,
    })
})

//分页查询列表+姓名+学号模糊查询:
router.get('/listSeatlog', async(req, res) => {
    console.log(req.query)
    page = parseInt(req.query.page);
    limit = parseInt(req.query.limit);
    const total = await Seatslog.countDocuments({ nickname: { $regex: req.query.nickname, $options: 'i' }, username: { $regex: req.query.username } })
    const data = await Seatslog.find({ nickname: { $regex: req.query.nickname, $options: 'i' }, username: { $regex: req.query.username } }).sort({ _id: -1 }).skip((page - 1) * limit).limit(limit)
    res.send({
        status: "ok",
        data: data,
        total
    })
})

//以下为测试接口

//删库-所有占座记录
router.post('/test1', async(req, res) => {
    await Seatslog.deleteMany();
    res.send({
        status: "ok",
    })
})

//删库-所有座位
router.post('/test2', async(req, res) => {
    await Seats.deleteMany();
    res.send({
        status: "ok",
    })
})

//分页查询+占座人模糊查询:占座记录列表
router.get('/oldListSeatlog', async(req, res) => {
    page = parseInt(req.query.page);
    limit = parseInt(req.query.limit);
    const total = await Seatslog.countDocuments({ $or: [{ "nickname": { $regex: req.query.nickname, $options: 'i' } }] })
    const data = await Seatslog.find({ $or: [{ "nickname": { $regex: req.query.nickname, $options: 'i' } }] }).sort({ _id: -1 }).skip((page - 1) * limit).limit(limit)
    res.send({
        status: "ok",
        data: data,
        total
    })
})

//添加占座记录
router.post("/addSeatslog", async(req, res, next) => {
    const data = await Seatslog.create(req.body);
    res.send({
        status: "ok",
        data: data,
    })
});

router.get("/", (req, res) => {
    res.send("hello seat...");
});

module.exports = router;